﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL;
using Framework.Library.Core.Config;

namespace Framework.Library.Base.DataAccess
{
    public class PortalAccess
    {
        #region PortalManager

        public static void LoadPortals(List<PortalPortal> dtPortals, ListItemCollection lstItems)
        {
            if (dtPortals != null)
                LoadPortals(dtPortals, lstItems, 0);
        }

        public static void LoadPortals(List<PortalPortal> dtPortals, ListItemCollection lstItems, int parentID)
        {
            lstItems.Clear();
            if (dtPortals != null)
            {
                var _dtPortals = new List<PortalPortal>();
                _dtPortals = dtPortals.FindAll(l => (l.PORTAL_PARENTID == parentID));
                foreach (PortalPortal row in _dtPortals)
                {
                    var item = new ListItem(MiscUtility.StringIndent(0) + row.NAME, row.PORTALID.ToString());
                    lstItems.Add(item);
                    LoadPortals(dtPortals, lstItems, row.PORTALID, 0);
                }
            }
        }

        public static void LoadPortals(List<PortalPortal> dtPortals, ListItemCollection lstItems, int parentID,
                                       int level)
        {
            level++;
            if (dtPortals != null)
            {
                var _dtPortals = new List<PortalPortal>();
                _dtPortals = dtPortals.FindAll(l => (l.PORTAL_PARENTID == parentID));
                foreach (PortalPortal row in _dtPortals)
                {
                    var item = new ListItem(MiscUtility.StringIndent(level) + row.NAME, row.PORTALID.ToString());
                    lstItems.Add(item);
                    LoadPortals(dtPortals, lstItems, row.PORTALID, level);
                }
            }
        }

        #endregion

        #region MenuManager

        public static void LoadMenus(List<PortalMenu> dtRoles, ListItemCollection lstItems)
        {
            if (dtRoles != null)
                LoadMenus(dtRoles, lstItems, 0);
        }

        public static void LoadMenus(List<PortalMenu> dtRoles, ListItemCollection lstItems, int parentID)
        {
            lstItems.Clear();
            if (dtRoles != null)
            {
                var _dtPortals = new List<PortalMenu>();
                _dtPortals = dtRoles.FindAll(l => (l.PARENTID == parentID));
                foreach (PortalMenu row in _dtPortals)
                {
                    var item = new ListItem(MiscUtility.StringIndent(0) + row.NAME, row.ID.ToString());
                    lstItems.Add(item);
                    LoadMenus(dtRoles, lstItems, row.ID, 0);
                }
            }
        }

        public static void LoadMenus(List<PortalMenu> dtRoles, ListItemCollection lstItems, int parentID, int level)
        {
            level++;
            if (dtRoles != null)
            {
                var _dtPortals = new List<PortalMenu>();
                _dtPortals = dtRoles.FindAll(l => (l.PARENTID == parentID));
                foreach (PortalMenu row in _dtPortals)
                {
                    var item = new ListItem(MiscUtility.StringIndent(level) + row.NAME, row.ID.ToString());
                    lstItems.Add(item);
                    LoadMenus(dtRoles, lstItems, row.ID, level);
                }
            }
        }

        public static void LoadMenus(List<PortalMenu> dtRoles, TreeNodeCollection lstItems)
        {
            lstItems.Clear();
            if (dtRoles != null)
                LoadMenus(dtRoles, lstItems, 0);
        }

        public static void LoadMenus(List<PortalMenu> dtRoles, TreeNodeCollection lstItems, int parentID)
        {
            lstItems.Clear();
            if (dtRoles != null)
            {
                var _dtPortals = new List<PortalMenu>();
                _dtPortals = dtRoles.FindAll(l => (l.PARENTID == parentID));
                foreach (PortalMenu row in _dtPortals)
                {
                    var childNode = new TreeNode(row.NAME, row.ID.ToString());
                    childNode.Expanded = false;
                    lstItems.Add(childNode);
                    LoadMenus(dtRoles, childNode, row.ID, 0);
                }
            }
        }

        public static void LoadMenus(List<PortalMenu> dtRoles, TreeNode lstItems, int parentID, int level)
        {
            level++;
            if (dtRoles != null)
            {
                var _dtPortals = new List<PortalMenu>();
                _dtPortals = dtRoles.FindAll(l => (l.PARENTID == parentID));
                foreach (PortalMenu row in _dtPortals)
                {
                    var childNode = new TreeNode(row.NAME, row.ID.ToString());
                    childNode.Expanded = false;
                    lstItems.ChildNodes.Add(childNode);
                    LoadMenus(dtRoles, childNode, row.ID, level);
                }
            }
        }

        #endregion

        #region RoleManager

        public static void LoadRoles(List<PortalRole> dtRoles, ListItemCollection lstItems)
        {
            if (dtRoles != null)
                LoadRoles(dtRoles, lstItems, 0);
        }

        public static void LoadRoles(List<PortalRole> dtRoles, ListItemCollection lstItems, int parentID)
        {
            lstItems.Clear();
            if (dtRoles != null)
            {
                var _dtRoles = new List<PortalRole>();
                _dtRoles = dtRoles.FindAll(l => (l.ROLEPARENTID == parentID));
                foreach (PortalRole row in _dtRoles)
                {
                    var item = new ListItem(row.ROLENAME, row.ROLEID.ToString());
                    item.Attributes.Add("Level", "0");
                    lstItems.Add(item);
                    LoadRoles(dtRoles, lstItems, item);
                }
            }
        }

        private static void LoadRoles(List<PortalRole> dtRoles, ListItemCollection lstItems, ListItem curItem)
        {
            int curId = Convert.ToInt32(curItem.Value);
            int level = Convert.ToInt32(curItem.Attributes["Level"]);
            level++;
            var _dtRoles = new List<PortalRole>();
            _dtRoles = dtRoles.FindAll(l => (l.ROLEPARENTID == curId));
            foreach (PortalRole row in _dtRoles)
            {
                var item = new ListItem(MiscUtility.StringIndent(level) + row.ROLENAME, row.ROLEID.ToString());
                item.Attributes.Add("Level", level.ToString());
                lstItems.Add(item);
                LoadRoles(dtRoles, lstItems, item);
            }
        }

        public static void LoadRoles(List<PortalRole> dtRoles, TreeNodeCollection lstItems)
        {
            lstItems.Clear();
            if (dtRoles != null)
                LoadRoles(dtRoles, lstItems, 0);
        }

        public static void LoadRoles(List<PortalRole> dtRoles, TreeNodeCollection lstItems, int parentID)
        {
            lstItems.Clear();
            if (dtRoles != null)
            {
                var _dtPortals = new List<PortalRole>();
                _dtPortals = dtRoles.FindAll(l => (l.ROLEPARENTID == parentID));
                foreach (PortalRole row in _dtPortals)
                {
                    var childNode = new TreeNode(row.ROLENAME, row.ROLEID.ToString());
                    childNode.Expanded = false;
                    lstItems.Add(childNode);
                    LoadRoles(dtRoles, childNode, row.ROLEID, 0);
                }
            }
        }

        public static void LoadRoles(List<PortalRole> dtRoles, TreeNode lstItems, int parentID, int level)
        {
            level++;
            if (dtRoles != null)
            {
                var _dtPortals = new List<PortalRole>();
                _dtPortals = dtRoles.FindAll(l => (l.ROLEPARENTID == parentID));
                foreach (PortalRole row in _dtPortals)
                {
                    var childNode = new TreeNode(row.ROLENAME, row.ROLEID.ToString());
                    childNode.Expanded = false;
                    lstItems.ChildNodes.Add(childNode);
                    LoadRoles(dtRoles, childNode, row.ROLEID, level);
                }
            }
        }

        #endregion

        #region LanguageManager

        public static void LoadLanguages(List<PortalLanguage> dtPortals, ListItemCollection lstItems)
        {
            lstItems.Clear();
            if (dtPortals != null)
            {
                foreach (PortalLanguage row in dtPortals)
                {
                    var item = new ListItem(MiscUtility.StringIndent(0) + row.LANGUAGE_NAME, row.LANGUAGE_CULTURE.ToString());
                    lstItems.Add(item);
                }
            }
        }

        #endregion

        #region TemplateManager

        public static void LoadTemplates(List<PortalTemplate> dtPortals, ListItemCollection lstItems)
        {
            lstItems.Clear();
            if (dtPortals != null)
            {
                foreach (PortalTemplate row in dtPortals)
                {
                    var item = new ListItem(row.NAME, row.BASEPATH);
                    lstItems.Add(item);
                }
            }
        }

        #endregion

        #region UserManager

        public static void LoadUsers(List<PortalUser> dtPortals, ListItemCollection lstItems)
        {
            lstItems.Clear();
            if (dtPortals != null)
            {
                foreach (PortalUser row in dtPortals)
                {
                    var item = new ListItem(MiscUtility.StringIndent(0) + row.USERNAME, row.USERID.ToString());
                    lstItems.Add(item);
                }
            }
        }

        #endregion
    }
}